package com.aprivate.kn.ratingsystem.daoutil;

import android.content.Context;
import android.util.Log;

import com.aprivate.kn.ratingsystem.dao.DaoManager;
import com.aprivate.kn.ratingsystem.dao.ElementsDao;
import com.aprivate.kn.ratingsystem.dao.UserDao;
import com.aprivate.kn.ratingsystem.daoBean.Elements;
import com.aprivate.kn.ratingsystem.daoBean.User;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by kenan on 2018/11/29.
 */

public class ElementDaoUtil {
    private DaoManager mManager;
    private String TAG="flag";
    //操作Fruit的构造函数
    public ElementDaoUtil(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }
    /**
     * 完成user记录的插入，如果表未创建，先创建user表
     *
     * @param elements
     * @return  返回long值 -1为失败
     */
    public boolean insertUser(Elements elements) {
        boolean flag = false;
        flag = mManager.getDaoSession().getElementsDao().insertOrReplace(elements) == -1 ? false : true;
        Log.i(TAG, "-------插入一条的结果为"+flag);
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     * @return
     */
    public boolean insertListElement(final List<Elements> mList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (Elements elements : mList) {
                        mManager.getDaoSession().insertOrReplace(elements);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     *   刷新指定数据
     */
    public boolean reFreshUser(Elements elements){
        boolean flag = false;
        mManager.getDaoSession().refresh(elements);
        return flag;
    }
    /**
     * 修改一条数据
     * @return
     */
    public boolean updateElements(Elements elements){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(elements);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     * @return
     */
    public boolean deleteUser(Elements user){
        boolean flag = false;
        try {
            mManager.getDaoSession().delete(user);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     * @return
     */
    public boolean deleteAll(){
        boolean flag = false;
        try {
            mManager.getDaoSession().deleteAll(Elements.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<Elements> queryAllUser(){
        return mManager.getDaoSession().loadAll(Elements.class);
    }

    /**
     * 根据主键id查询记录
     * @param key
     * @return
     */
    public Elements queryUserById(String key){
        return mManager.getDaoSession().load(Elements.class, key);
    }

    /**
     * 使用 sql语句进行查询操作
     *  参数一sql语句  参数二查询条件限定
     */
    public List<Elements> queryUserBySql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(Elements.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     * @return
     */
    public List<Elements> queryUserByQueryBuilder(long id){
        QueryBuilder<Elements> queryBuilder = mManager.getDaoSession().queryBuilder(Elements.class);
        return queryBuilder.where(ElementsDao.Properties.Id.eq(id)).list();
    }
    /**
     * 使用queryBuilder进行查询
     * @return
     */
    public List<Elements> queryElmentsByQueryBuilder(String Productinfo_id){
        QueryBuilder<Elements> queryBuilder = mManager.getDaoSession().queryBuilder(Elements.class);
        return queryBuilder.where(ElementsDao.Properties.Productinfo_id.eq(Productinfo_id)).list();
    }

      /* * 使用queryBuilder进行查询  项目id   fid
     * @return
             */
    public List<Elements> queryElmentsByQueryBuilder(Long Productinfo_id, String  fid){
        QueryBuilder<Elements> queryBuilder = mManager.getDaoSession().queryBuilder(Elements.class);
        return queryBuilder.where(ElementsDao.Properties.Productinfo_id.eq(Productinfo_id),ElementsDao.Properties.FId.eq(fid) ).list();
    }
}
